package com.itheima.d7_thread_communication;

public class Cook implements Runnable{
    @Override
    public void run() {
//        while (true){
//            synchronized (Desk.class){
//                if(!Desk.flag){
//                    System.out.println(Thread.currentThread().getName() +"蒸包子");
//                    Desk.flag = true;
//                    try {
//                        Thread.sleep(50);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                    // 通知吃货吃包子
//                    Desk.class.notify();
//                    // 厨师自己等待
//                    try {
//                        Desk.class.wait();
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                }else {
//                    System.out.println("厨师白跑一趟");
//                }
//            }
//        }



        while (true) {
            synchronized (Desk.class) {
                while (Desk.flag) {
                    System.out.println("厨师白跑一趟");
                    try {
                        Desk.class.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println(Thread.currentThread().getName() + "蒸包子");
                Desk.flag = true;
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // 通知所有吃货吃包子
                Desk.class.notifyAll();
            }
        }
    }
}
